.PHONY: default help build run clean

PROJECT_NAME=scm
JAR_FILE=target/scm.jar
IMAGE_NAME := registry.cn-hangzhou.aliyuncs.com/lujianwen/directive
SIT_IMAGE_NAME := registry.cn-shenzhen.aliyuncs.com/wsyx/scm-sit

GIT_COMMIT=$(shell git rev-parse HEAD)

default: help

help:
	@echo 'Management commands:'
	@echo
	@echo 'Usage:'
	@echo '    make build           Compile the project.'
	@echo '    make run             Run the project.'
	@echo '    make clean           Clean the project.'
	@echo


build:
	@mvn compile
	@mvn package -Dmaven.test.skip=true

clean:
	@mvn clean

gen:
	@mvn mybatis-generator:generate

dep:
	@mvn -Dverbose dependency:tree

run:
	@mvn clean
	@mvn compile
	@mvn clean package -Dmaven.test.skip=true
	@java -jar ${JAR_FILE} --spring.profiles.active=local

package:
ifndef tag
	@echo "tag variable missing"
	exit 1
endif
	@echo "packing tag $(tag)"
	#@git checkout $(tag)
	@mvn clean
	@mvn compile
	@mvn package -Dmaven.test.skip=true
	@echo "building image $(IMAGE_NAME) $(GIT_COMMIT)"
	@docker build --build-arg GIT_COMMIT=$(GIT_COMMIT) -t $(IMAGE_NAME):$(tag) .
	@docker push $(IMAGE_NAME):$(tag)
	#@git checkout release

deploy:
ifndef tag
	@echo "tag variable missing"
	exit 1
endif
	@echo "deploy tag $(tag)"
	docker pull $(IMAGE_NAME):$(tag)
	docker stop scm
	docker rm scm
	docker run --add-host api.irsnp.com:218.13.19.230 -d -p 9051:9051 -e "SPRING_PROFILES_ACTIVE=prod" -v /var/log/scm:/var/log/scm --name scm $(IMAGE_NAME):$(tag)

package-sit:
	@git pull
	@mvn clean
	@mvn compile
	@mvn package -Dmaven.test.skip=true
	@echo "building image $(SIT_IMAGE_NAME) $(GIT_COMMIT)"
	docker build --build-arg GIT_COMMIT=$(GIT_COMMIT) -t $(SIT_IMAGE_NAME):latest .
	docker push $(SIT_IMAGE_NAME):latest

sit:
	@git pull
	@mvn clean
	@mvn compile
	@mvn package -Dmaven.test.skip=true
	@echo "building image $(SIT_IMAGE_NAME) $(GIT_COMMIT)"
	docker build --build-arg GIT_COMMIT=$(GIT_COMMIT) -t $(SIT_IMAGE_NAME):latest .
	docker push $(SIT_IMAGE_NAME):latest
	docker pull $(SIT_IMAGE_NAME):latest
	docker stop scm-sit
	docker rm scm-sit
	docker run --add-host api.irsnp.com:218.13.19.230 -d -p 9051:9051 -e "SPRING_PROFILES_ACTIVE=sit" -v /var/log/scm:/var/log/scm --name scm-sit $(SIT_IMAGE_NAME):latest



